pipeline {
    agent none // 不指定全局代理
    stages {
        stage('Initialize') {
            steps {
                script {
                    def repositories = []
                    def isTimerTrigger = false
                    currentBuild.getBuildCauses().each { cause ->
                        def shortDescription = "${cause.shortDescription}"
                        if (shortDescription.contains('Started by timer')) {
                            isTimerTrigger = true
                        }
                    }
                     def jsonString = "${params.TEST_PARAM}"
                    if (isTimerTrigger) {
                        echo 'This build was triggered by a timer.'
                        getDatabaseConnection(type:'GLOBAL') {
                            def result = sql(sql:  "SELECT * FROM project_info where project_name = '${env.JOB_NAME}';")
                            jsonString = "${result[0].param}"
                        }
                    } 
                    repositories = readJSON text: jsonString
                    def parallelStages = [:]
                    for (repo in repositories) {
                        def repositoryName = "${repo.repositoryName}"
                        def cript = "${repo.script}"
                        parallelStages[repositoryName.trim()] = {
                            evaluate(cript)
                        }
                    }
                    print "${params.TEST_PARAM}"
                    print "$parallelStages"
                    parallel parallelStages
                }
            }
        }
    }

    post {
        success {
            script {
                dingtalk(
                        robot: 'ci',
                        type: 'MARKDOWN',
                        title: "success: ${env.JOB_NAME}",
                        text: [
                                '### 构建信息',
                                '---',
                                "- 项目名称 : ${env.JOB_NAME}",
                                "- 构建编号: <font color=#008000>${env.BUILD_ID}</font>",
                                "- 构建状态: **<font color=#008000>${currentBuild.result}</font>**",
                                "- 项目地址: [地址](${env.BUILD_URL})",
                                "- 构建日志: [日志](${env.BUILD_URL}console)",
                                '- 进入页面: [http://43.138.159.33:65004/](http://43.138.159.33:65004/)',
                                '',
                                '---',
                                "- 执行人: ${currentBuild.getBuildCauses().shortDescription[0]}",

                                '---',
                                "- 持续时间:${currentBuild.durationString}",

                                '---'
                        ]
                )
            }
        }
        failure {
            script {
                dingtalk(
                        robot: 'ci',
                        type: 'MARKDOWN',
                        title: "failure: ${env.JOB_NAME}",
                        text: [
                                '### 构建信息',
                                '---',
                                "- 项目名称 : ${env.JOB_NAME}",
                                "- 构建编号: <font color=#dd3238>${env.BUILD_ID}</font>",

                                "- 构建状态: **<font color=#dd3238>${currentBuild.result}</font>**",
                                "- 项目地址: [地址](${env.BUILD_URL})",
                                "- 构建日志: [日志](${env.BUILD_URL}console)",
                                 '- 进入页面: [http://43.138.159.33:65004/](http://43.138.159.33:65004/)',

                                '',
                                '---',
                                "- 执行人: ${currentBuild.getBuildCauses().shortDescription[0]}",

                                '---',
                                "- 持续时间:${currentBuild.durationString}",

                                '---'
                        ]
                )
            }
        }
    }
}
